require(['config'],()=>{
    require(['jquery','header','footer'],($)=>{
        class shopcar{
            constructor(){
                this.rander()
                this.donum()
                this.select()
                this.calcsum()
                this.del()
                this.delall()
            }
            rander(){
                var local=JSON.parse(localStorage.getItem('cart'))
                for(var i=0;i<local.length;i++){
                    var sum=Number(local[i].price.slice(1))*Number(local[i].num)
                    $('#car').append(`
                        <li data-id="${local[i].id}">
                        <input type="checkbox" ${local[i].checked&&'checked'}>
                        <span><img src="${local[i].img}" alt="">${local[i].title}</p></span>
                        <span>${local[i].price.slice(1)}</span>
                        <span><button class="cut">-</button><i>${local[i].num}</i><button class="add">+</button></span>
                        <span class="sum">${sum}</span>
                        <span class="del">删除</span>
                        </li>
                    `)
                }
                $('.content h1 span').text(local.length)
                $('.carsubmit .right span:first').text(local.length)
                this.local=local
            }
            donum(){
                const _this=this
                //加加减减和总计的计算
                $('.cut').on('click',function(){
                    if($(this).next().text()>1){
                        $(this).next().text($(this).next().text()-1);   
                        var sum=$(this).next().text()*$(this).parent().prev().text()
                        $(this).parent().next().text(sum)
                        const id=$(this).parents('li').data('id')
                        _this.local=_this.local.map((shop)=>{
                            if(shop.id==id){
                                shop.num--;  
                        }
                        return shop
                    })
                    _this.calcsum()
                    }
                })
                $('.add').on('click',function(){
                    $(this).prev().text(Number($(this).prev().text())+1);
                    var sum=$(this).prev().text()*$(this).parent().prev().text()
                    $(this).parent().next().text(sum)
                    // console.log(_this.local)
                    const id=$(this).parents('li').data('id')
                        _this.local=_this.local.map((shop)=>{
                            if(shop.id==id){
                                shop.num++;  
                        }
                        return shop
                    })
                    _this.calcsum()
                })
                
            }

            select(){
                //全选
                const _this=this
                $('#car li input').on('change',function(){ 
                    const id=$(this).parents('li').data('id')
                    _this.local=_this.local.map((shop)=>{
                        if(shop.id==id){
                            shop.checked=$(this).prop('checked')   
                        }
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.local))               
                    const isall=_this.local.every((shop)=>{
                        return shop.checked
                    })
                    $('.cartitle input').prop("checked",isall);
                    
                    _this.calcsum()
                    var num=0;
                    _this.local.forEach(item => {
                        // console.log(item.checked)
                        if(item.checked==true){
                            num++; 
                        }
                        return num;
                    });
                    $('.carsubmit .right span:first').text(num)

                })
                $('.cartitle input').on('change',function(){
                    // console.log(this.checked)
                    $('#car li input').prop("checked",this.checked)
                    if(this.checked){
                        _this.calcsum()
                        $('.carsubmit .right span:first').text(_this.local.length)
                    }else{
                        $('#allprice').text(0)
                        $('.carsubmit .right span').text(0)
                    }
                })
            }

            calcsum(){
                // 计算总数
                let total=this.local.reduce((item,shop)=>{
                    if(shop.checked){
                        item=item+shop.price.slice(1)*shop.num
                    }
                    return item
                },0)
                $('#allprice').text(total)
            }

            del(){
                const _this=this
                $('.del').on('click',function(){
                    $(this).parents('li').remove()
                    const id=$(this).parents('li').data().id
                 
                    _this.local=_this.local.filter((shop)=>{
                    return id!=shop.id
                    })
                    // console.log( _this.local)
                    localStorage.setItem('cart',JSON.stringify(_this.local)) 
                })
                
            }

            delall(){
                $('.carsubmit .left p').on('click',function(){
                    localStorage.removeItem('cart')
                    $('#car li').remove()
                    $('.content h1 span').text(0)
                    $('.carsubmit .right span:first').text(0)
                    $('#allprice').text(0)

                })
            }



        }
        new shopcar()
    })
})